对01字符串进行一些操作,01字符串上的0和0相邻时会变成1,而1和1相邻时会在字符串上消失,而0和1相邻时什么都不会发生,问这个字符串最后会变成什么样。
"00110001"
"01"
00110001→1110001→10001→1101→01
,字符串上的合并消失应优先与左边进行,例如000,中间的0优先与左边的0合并变为10,消失同理
class Solution: def solve(self , str ): # write code here stack = [] for c in str: if not stack: stack.append(c) else: if c == '0': if stack[-1] == '0': stack.pop(-1) tag = 0 while stack and stack[-1] == '1': tag = 1 stack.pop(-1) if not tag: stack.append('1') else: stack.append(c) else: if stack[-1] == '1': stack.pop(-1) else: stack.append(c) return ''.join(stack)